<template>
  <div class="container">
    <el-alert :title="'金额统一为万元,保留4位小数'" type="info" effect="light" show-icon closable></el-alert>
    <el-form :model="formList" ref="formList" label-position="top" :rules="rules" :inline="false" size="normal">
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="名称" prop="word_name">
            <el-input v-model="formList.word_name" maxlength="50" show-word-limit></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="类型" prop="type">
            <el-cascader :options="d_serveAll" v-model="formList.type" placeholder="" class="wid">
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0" v-if="otherShow">
          <el-form-item label="其他类型" prop="word_type_other">
            <el-input v-model="formList.word_type_other" maxlength="50"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="申请金额" prop="service_pre_amount">
            <el-input-number v-model="formList.service_pre_amount" :min="0" :step="1" :precision="4" class="wid">
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="审核金额" prop="third_check_amount">
            <el-input-number v-model="formList.third_check_amount" :min="0" :step="1" :precision="4" class="wid">
            </el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-alert title="清单内容上线5000字，超出请将清单分开填写" type="error" effect="light" :closable="false" show-icon></el-alert>
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="清单内容" prop="word_list">
            <el-input v-model="formList.word_list" maxlength="5000" type="textarea" :rows="10" placeholder="字数上限为5000字"
              show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="center">
      <el-button type="danger" size="medium" @click="cancel">取消</el-button>
      <template v-if="this.switch">
        <el-button type="primary" size="medium" @click="onEdit" :loading="saveLoading">保存</el-button>
      </template>
    </div>
  </div>
</template>
<script>
import { d_serveAll } from '@/utils/dicts.js'
export default {
  name: 'BidWordList',
  components: {

  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    source: {
      type: String,
      default: () => {
        return '1'
      }
    },
    formList: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  data () {
    return {
      saveLoading: false,
      d_serveAll: d_serveAll,
      rules: {
        word_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        word_type_other: [
          { required: true, message: '请输入其他类型', trigger: 'blur' }
        ],
        service_pre_amount: [
          { required: true, validator: this.$util.isFloat, trigger: 'blur' }
        ],
        third_check_amount: [
          { required: true, validator: this.$util.isFloat, trigger: 'blur' }
        ],
        word_list: [
          { required: true, message: '请填写工程量清单内容，不超过5000字', trigger: 'blur' }
        ],
      },
    }
  },
  created () {

  },
  mounted () {
  },
  methods: {
    async onEdit () {
      this.saveLoading = true
      this.$refs['formList'].validate(async (valid) => {
        if (valid) {
          const type = JSON.stringify(this.formList.type)
          const res = await this.$http('blockWordUp', {
            del_falg: '0',
            source: this.source,
            subject_id: this.formList.subject_id,
            id: this.formList.id,
            word_name: this.formList.word_name,
            word_type: type,
            word_type_other: this.formList.word_type_other,
            service_pre_amount: this.formList.service_pre_amount,
            third_check_amount: this.formList.third_check_amount,
            word_list: this.formList.word_list
          })
          this.$tips.success(res.errmsg)
          this.$emit('cancel')
          this.$emit('init')
          this.saveLoading = false
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      })
    },
    cancel () {
      this.$emit('cancel')
    }
  },
  computed: {
    otherShow () {
      let arr = this.formList.type
      let other = '17,80,227,328,354,360'
      if (arr) {
        let type = arr[arr.length - 1]
        if (other.indexOf(type) > -1) {
          return true
        } else {
          return false
        }
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
